﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Data;
using System.Net.Mail;
using System.Net;
public class ManageBase<T> where T:new ()
{
    public static T Get(int ID, bool SearchDeleted = false)
    {
        T model=new T();
        DataTable dt = DBHelper.GetDataTable("select * from [" + model.GetType().Name + "] where ID=" + ID + (SearchDeleted ? "" : " and IsDel=0"));
        if (dt.Rows.Count == 0)
            return default(T);
        else
        {
            Type type = model.GetType();
            PropertyInfo[] pil = type.GetProperties();
            for (int i = 0; i < pil.Length; i++)
            {
                pil[i].SetValue(model, DBNull.Value == dt.Rows[0][pil[i].Name] ? "" : dt.Rows[0][pil[i].Name], null);
            }
        }
        return model;
    }
    public static T Get(string Key, string Value, bool SearchDeleted = false)
    {
        T model = new T();
        DataTable dt = DBHelper.GetDataTable("select top 1 * from [" + model.GetType().Name + "] with(nolock) where [" + Key + "]='" + Value.Replace("'","''") + "'" + (SearchDeleted ? "" : " and IsDel = 0"));
        return Get(dt);
    }
    public static T Get(DataTable dt)
    {
        T model = new T();
        if (dt.Rows.Count == 0)
            return default(T);
        else
        {
            Type type = model.GetType();
            PropertyInfo[] pil = type.GetProperties();
            for (int i = 0; i < pil.Length; i++)
            {
                pil[i].SetValue(model, DBNull.Value == dt.Rows[0][pil[i].Name] ? "" : dt.Rows[0][pil[i].Name], null);
            }
        }
        return model;
    }
    public static List<T> GetList(DataTable dt)
    {
        List<T> list = new List<T>();
        if (dt.Rows.Count == 0)
        {
            return list;
        }
        else
        {
            Type type = new T().GetType();
            PropertyInfo[] pil = type.GetProperties();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                T model = new T();
                for (int j = 0; j < pil.Length; j++)
                {
                    pil[j].SetValue(model, DBNull.Value == dt.Rows[i][pil[j].Name] ? "" : dt.Rows[i][pil[j].Name], null);
                }
                list.Add(model);
            }
        }
        return list;
    }
    public static void Del(int ID, bool dataUtility = false)
    {
        T model = new T();
        if(dataUtility)
            DBHelper.ExecuteCommand("Delete ["+model.GetType().Name+"] where ID="+ID);
        else
            DBHelper.ExecuteCommand("Update [" + model.GetType().Name + "] set IsDel=1 where ID="+ID);
    }
        
}
public class ManageBase
{
    public static void Add(object o)
    {
        Type type = o.GetType();
        string name = "";
        string value = "";
        PropertyInfo[] pil = type.GetProperties();
        for (int i = 1; i < pil.Length; i++)
        {
            if (pil[i].GetValue(o, null) != null)
            {
                name += "[" + pil[i].Name + "],";
                value += "'" + pil[i].GetValue(o, null).ToString().Replace("'", "''") + "',";
            }
        }
        type.GetProperty("ID").SetValue(o, int.Parse(DBHelper.GetDataTable("begin tran insert into [" + type.Name + "](" + name.Remove(name.Length - 1) + ") values(" + value.Remove(value.Length - 1) + ")  select SCOPE_IDENTITY() commit tran").Rows[0][0].ToString()), null);
    }
    public static void Set(object o)
    {
        Type type = o.GetType();
        string SQL = "";
        PropertyInfo[] pil = type.GetProperties();
        for (int i = 1; i < pil.Length; i++)
        {
            if (pil[i].GetValue(o, null) != null)
                SQL += "[" + pil[i].Name + "]='" + pil[i].GetValue(o, null).ToString().Replace("'", "''") + "',";
        }

        DBHelper.ExecuteCommand("Update [" + type.Name + "] set " + SQL.Remove(SQL.Length - 1) + " where ID=" + pil[0].GetValue(o, null));
    }


}